var wineApp = wineApp || {};
wineApp.tjtc = (function() {
    var initPage = function() {
        loadPackInfo();
        initEvent();
    }
    var initEvent = function() {
            $(document).on('click', ".wine_order_confirm", function(event) {
                var selectSet = $('input[name=pack]:checked').val();
                var selectProd = _.filter(mapTaochan, function(taocan) {
                    return taocan.index == selectSet;
                });
                wineApp.orderutil.createProdObjToStorage(selectProd, 'order');
                window.location.href = 'order.html';
            }).on('click', '#gwc', function(event) {
                var selectSet = $('input[name=pack]:checked').val();
                var selectProd = _.filter(mapTaochan, function(taocan) {
                    return taocan.index == selectSet;
                });
                var ajaxUrl = wineApp.CONFIG.API.cartadd;
                $.each(selectProd, function(index, taocan) {
                    var selectedProd = {
                        prodid: taocan.id,
                        proname: taocan.wineName,
                        titleimg: wineApp.common.getImgName(taocan.wineImg),
                        num: taocan.count,
                        oprice: taocan.wineOverseasPrice,
                        price: taocan.winePrice
                    }
                    console.log("selectedProd:%O", selectedProd);
                    wineApp.ajax.post(ajaxUrl, {
                        product: selectedProd
                    }).done(function(data) {
                        window.location.href = "gwc.html";
                    });
                });
            });
        }
        //保存套餐信息，用于选择套餐时，生成订单数据，在页面加载的时候，初始化该数据
    var mapTaochan = [];
    var loadPackInfo = function() {
        var template = '<tr>' +
            '<th>#tcname#</th>' +
            '<td>#detail#</td>' +
            '<td>#sumfee#元</td>' +
            '<td><input name="pack" type="radio" value="#index#"></td>' +
            '</tr>'
        var ajaxUrl = wineApp.CONFIG.API.packinfo;
        //var orders = [];
        wineApp.ajax.post(ajaxUrl, {}).done(function(data) {
            var resourceURL = wineApp.tool.getResourceURL();
            $.each(data.list, function(index, val) {
                var tmp = template;
                var param = {}
                param.tcname = val.tcname;
                param.sumfee = val.sumfee;
                param.index = index;
                var detail = ""
                $.each(val.detail, function(detailIndex, wine) {
                    detail += wine.title + "*" + wine.wcount + wine.unit + "<br>"
                    var saveToOrder = {};
                    saveToOrder.count = wine.wcount;
                    saveToOrder.prodid = wine.prodid;
                    saveToOrder.ammount = wine.wsumfee;
                    saveToOrder.wineName = wine.title;
                    saveToOrder.wineImg = resourceURL + "/" + wine.titleimg;
                    saveToOrder.winePrice = wine.price;
                    saveToOrder.wineOverseasPrice = wine.oprice;
                    saveToOrder.id = wine.prodid;
                    saveToOrder.index = index;
                    mapTaochan.push(saveToOrder)
                });

                param.detail = detail;
                for (var prop in param) {
                    var reg = new RegExp("#" + prop + "#", "gm");
                    tmp = tmp.replace(reg, param[prop]);
                }
                $('.gTable').append(tmp);
            });

        })
    }
    return {
        initPage: initPage
    }
})();

$(document).ready(function() {
    wineApp.common.initHeaderIcon(0, false);
    wineApp.tjtc.initPage();
});